package com.elinkway.infinitemovies.async;

import android.content.Context;

import com.elinkway.infinitemovies.bean.OutSiteStreamInfo;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.OutSiteStreamInfoParser;
import com.lvideo.http.bean.LVideoDataHull;

import java.util.List;

/**
 * Created by wangyemin on 2015/12/7.
 */
public class RequestOutSiteStreamInfoTask extends MoviesHttpAsyncTask<OutSiteStreamInfo> {

    private String url;
    private int type;
    private String formats = "";
    private List<String> mApiContent = null;

    public String getFormats() {
        return formats;
    }

    public void setFormats(String formats) {
       this.formats = formats;
    }

    private RequestResultListener<OutSiteStreamInfo> listener;

    public RequestOutSiteStreamInfoTask(Context context) {
        super(context);
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setListener(RequestResultListener<OutSiteStreamInfo> listener) {
        this.listener = listener;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setApiContent(List<String> apiContent) {
        mApiContent = apiContent;
    }

    @Override
    public LVideoDataHull<OutSiteStreamInfo> doInBackground() {
        return MoviesHttpApi.requestOutSiteStreamInfo(new OutSiteStreamInfoParser(type), url, formats, mApiContent);
    }

    @Override
    public void onPostExecute(int updateId, OutSiteStreamInfo result) {
        if (listener != null)
            listener.onRequestSuccess(updateId, result);
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        super.netErr(updateId, errMsg);
        if (null != listener) {
            listener.onRequestFailed();
        }
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        super.dataNull(updateId, errMsg);
        if (null != listener) {
            listener.onRequestFailed();
        }
    }

    @Override
    public void netNull() {
        super.netNull();
        if (null != listener) {
            if (listener instanceof NewRequestResultListener) {
                ((NewRequestResultListener) listener).onNoNet();
            }
        }
    }

    @Override
    public void codeErr(String code, String msg) {
        super.codeErr(code, msg);
        if (null != listener) {
            listener.onRequestFailed();
        }
    }
}
